<?php

namespace common\models;

use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;

/**
 * This is the model class for table "{{%vip_order}}".
 *
 * @property integer $id
 * @property integer $user_id
 * @property string $money
 * @property integer $package_id
 * @property string $order_sn
 * @property integer $status
 * @property PayLog $log
 * @property integer $created_at
 */
class VipOrder extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%vip_order}}';
    }
    
    public function behaviors(){
        return [
            [
                'class' => TimestampBehavior::className(),
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at']
                ]
            ]
        ];
    }
    
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['user_id', 'money', 'package_id', 'order_sn'], 'required'],
            [['user_id', 'package_id', 'status', 'created_at'], 'integer'],
            [['money'], 'number'],
            [['order_sn'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'user_id' => 'User ID',
            'money' => 'Money',
            'package_id' => 'Package ID',
            'order_sn' => 'Order Sn',
            'status' => 'Status',
            'created_at' => 'Created At',
        ];
    }
    
    public function getLog()
    {
        return $this->hasOne(PayLog::className(), ['order_sn' => 'order_sn']);
    }
   
    public static function findByOrderSn($order_sn)
    {
        return static::findOne(['order_sn' => $order_sn]);
    }
    
}
